package action;

import processing.core.PApplet;
import processing.core.PVector;
import run.Doodler;
import run.Env;
import run.LineCell;

public class TurnAction implements Action {
		private final PApplet p =Doodler.instance;
		private final Env e = Env.instance;
		final int time;
		private int count = 0;
		float t;

		public TurnAction() {
			time = (int) p.random(25,100);
			t = p.random(-0.075F, 0.075F);
		}

		public boolean isActive() {
			if (count < time)
				return true;
			else return false;
		}

		public void doAction(LineCell lineCell, Float scale) {
			PVector dir = lineCell.dir;
			// Rotate by t rads
			if (isActive()) {
			    dir.x = dir.x * (float) Math.cos(t) - dir.y * (float) Math.sin(t);
			    dir.y = dir.x * (float) Math.sin(t) + dir.y * (float) Math.cos(t);
			    count++;

			    // randomly decide to spawn off on a tangent
			    //TODO danger is that the tangent will collide back and reproduce...
			    if (p.random(1F) < 0.025) {
				    PVector loc = lineCell.loc;
				    e.addCell(new LineCell(new PVector(loc.x + dir.x,loc.y + dir.y,0)
				    , new PVector(dir.x, dir.y, 0), lineCell.generation + 1));
			    }
			    
			}
		}
	}
